package com.health.ts.controller.register;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.register.Register;
import com.health.ts.entity.register.Remote;
import com.health.ts.entity.register.VRegisterList;
import com.health.ts.entity.register.VRemoteList;
import com.health.ts.service.register.RegisterService;
import com.health.ts.service.register.RemoteListService;
import com.health.ts.service.register.RemoteService;
import com.health.ts.service.register.VRegisterListService;
import com.health.ts.utils.AssertUtil;
import com.health.ts.utils.SessionUserUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2017-07-05
 */
@RestController
@RequestMapping(value = "/api/register",produces= BaseController.JSON_UTF8)
public class RegisterController extends BaseController{
	
	@Autowired
	RegisterService registerService;
	
	@Autowired
	VRegisterListService  vregisterListService;
	
	@Autowired
	RemoteListService remoteListService;
	
	@Autowired
	RemoteService remoteService;
	
	//挂
	@RequestMapping(value="/registerDone", method=RequestMethod.POST)
	public ApiResult addRegister(@RequestBody Register register) {
		return ApiResult.success(registerService.register(register));
	}
	
	/**
	 * 远程预约
	 * @param register
	 * @return
	 */
	@RequestMapping(value="/registerRemote", method=RequestMethod.POST)
	public ApiResult addRemoteRegister(@RequestBody Register register) {
		registerService.registerRemote(register);
		return ApiResult.success();
	}
	
	/**
	 * 退号
	 * @param pid
	 * @param rid
	 * @return
	 */
	@RequestMapping(value="/unRegister/{pid}/{rid}", method=RequestMethod.PUT)
	public ApiResult unRegister(@PathVariable String pid,@PathVariable String rid) {
		registerService.unRegister(pid,rid);
		return ApiResult.success();
	}
	
	/**
	 * 取消远程预约
	 * @param pid
	 * @param rid
	 * @return
	 */
	@RequestMapping(value="/unRemote/{pid}/{rid}", method=RequestMethod.PUT)
	public ApiResult unRemoteRegister(@PathVariable String pid,@PathVariable String rid) {
		registerService.unRemoteRegister(pid,rid);
		return ApiResult.success();
	}
	
	/**  
	* @Title: writeReport  
	* @Description: 写报告
	* @param @param rid
	* @param @param remote
	* @param @return 参数  
	* @return String 返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/writeReport/{rid}", method=RequestMethod.PUT)
	public ApiResult writeReport(@PathVariable String rid,@RequestBody Remote remote) {
		Remote info = remoteService.getById(rid);
		AssertUtil.notNull(info, "要修改的远程门诊记录不存在！");
		remote.setId(rid);
		remote.setUpdate_time(new Date());
		remote.setDiagnose(remote.getDiagnose());
		remote.setAdvice(remote.getAdvice());
		remote.setRemark(remote.getRemark());
		remote.setIsReport("1");//设置成已报告
		remoteService.updateById(remote);
		return ApiResult.success();
	}
	
	@RequestMapping(value="/select/{id}", method=RequestMethod.GET)
	public ApiResult selectReport(@PathVariable String id) {
		Remote remote=remoteService.getById(id);
		AssertUtil.notNull(remote, "要查询的远程门诊记录不存在！");
		return ApiResult.success(remote);
	}
	
	/**
	 * 预约列表查询
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/list", method=RequestMethod.POST)
	public ApiResult listReg(FrontPage page){
		QueryWrapper<VRegisterList> wrapper = new QueryWrapper<VRegisterList>();
		//名称
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			wrapper.like("patient_name", patient_name); 
		}
		//身份证号
		String idnumber =getParam(page,"idnumber");
		if (StringUtils.isNotEmpty(idnumber)){
			wrapper.eq("idnumber", idnumber); 
		}
		wrapper.eq("apply_docid", SessionUserUtil.getUserId()); 
		wrapper.eq("isReg", "0"); 
		Page<VRegisterList> pageList = vregisterListService.page(page.getPagePlus(), wrapper);
		CustomPage<VRegisterList> customPage = new CustomPage<VRegisterList>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 远程门诊查询待报告
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listUReport", method=RequestMethod.POST)
	public ApiResult listUnReport(FrontPage page){
		QueryWrapper<VRemoteList> wrapper = new QueryWrapper<VRemoteList>();
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			wrapper.like("patient_name", patient_name); 
		}
		String idnumber =getParam(page,"idnumber");
		if (StringUtils.isNotEmpty(idnumber)){
			wrapper.like("idnumber", idnumber); 
		}
		wrapper.eq("isReport", "0");//待报告
		wrapper.eq("target_docid",SessionUserUtil.getUserId());//远程预约医生
		Page<VRemoteList> pageList = remoteListService.page(page.getPagePlus(), wrapper);
		CustomPage<VRemoteList> customPage = new CustomPage<VRemoteList>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 远程门诊查询已报告
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listReport", method=RequestMethod.POST)
	public ApiResult listReport(FrontPage page){
		QueryWrapper<VRemoteList> wrapper = new QueryWrapper<VRemoteList>();
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			wrapper.like("patient_name", patient_name); 
		}
		String idnumber =getParam(page,"idnumber");
		if (StringUtils.isNotEmpty(idnumber)){
			wrapper.like("idnumber", idnumber); 
		}
		wrapper.eq("isReport", "1");//已报告
		wrapper.eq("target_docid",SessionUserUtil.getUserId());//远程预约医生
		Page<VRemoteList> pageList = remoteListService.page(page.getPagePlus(), wrapper);
		CustomPage<VRemoteList> customPage = new CustomPage<VRemoteList>(pageList);
		return ApiResult.success(customPage);
	}
}
